<script setup>
import { ref, computed } from 'vue'
import { useDietStore } from '../stores/diet'

/**
 * 获取饮食状态管理
 */
const dietStore = useDietStore()

/**
 * 当前选中的餐食类型
 */
const selectedMealType = ref('breakfast')

/**
 * 切换餐食类型
 * @param {string} type - 餐食类型
 */
const selectMealType = (type) => {
  selectedMealType.value = type
}

/**
 * 根据选中的餐食类型获取当前餐食列表
 */
const currentMealList = computed(() => {
  return dietStore.meals[selectedMealType.value] || []
})

/**
 * 根据餐食类型获取标题
 * @param {string} type - 餐食类型
 * @returns {string} - 标题
 */
const getMealTypeTitle = (type) => {
  const titles = {
    breakfast: '早餐',
    lunch: '午餐',
    dinner: '晚餐'
  }
  return titles[type] || '未知'
}

/**
 * 添加餐食到当日记录
 * @param {Object} meal - 餐食对象
 */
const addMeal = (meal) => {
  dietStore.addMeal(selectedMealType.value, {
    id: Date.now(),
    name: meal.name,
    calories: meal.calories,
    preparation: meal.preparation || '10分钟',
    image: meal.image
  })
}

/**
 * 移除餐食从当日记录
 * @param {number} mealId - 餐食ID
 */
const removeMeal = (mealId) => {
  dietStore.removeMeal(selectedMealType.value, mealId)
}
</script>

<template>
  <div class="diet-view">
    <!-- 顶部导航栏 -->
    <div class="nav-bar">
      <h1>饮食计划</h1>
    </div>
    
    <!-- 卡路里进度 -->
    <div class="calories-progress">
      <div class="progress-info">
        <div class="calories-text">
          <span class="current">{{ dietStore.caloriesIntake }}</span>
          <span class="separator">/</span>
          <span class="target">{{ dietStore.caloriesTarget }}</span>
          <span class="unit">kcal</span>
        </div>
        <div class="percentage">{{ dietStore.caloriesProgress }}%</div>
      </div>
      <div class="progress-bar">
        <div class="progress-filled" :style="`width: ${dietStore.caloriesProgress}%`"></div>
      </div>
      <div class="nutrition-info">
        <div class="nutrition-item">
          <span class="name">蛋白质</span>
          <span class="value">{{ dietStore.nutrition.protein }}g</span>
        </div>
        <div class="nutrition-item">
          <span class="name">脂肪</span>
          <span class="value">{{ dietStore.nutrition.fat }}g</span>
        </div>
        <div class="nutrition-item">
          <span class="name">碳水</span>
          <span class="value">{{ dietStore.nutrition.carbs }}g</span>
        </div>
      </div>
    </div>
    
    <!-- 餐食选择器 -->
    <div class="meal-selector">
      <div 
        v-for="type in ['breakfast', 'lunch', 'dinner']" 
        :key="type"
        class="meal-type" 
        :class="{ active: selectedMealType === type }"
        @click="selectMealType(type)"
      >
        <i class="fas" :class="{
          'fa-coffee': type === 'breakfast',
          'fa-utensils': type === 'lunch',
          'fa-moon': type === 'dinner'
        }"></i>
        <span>{{ getMealTypeTitle(type) }}</span>
        <div v-if="selectedMealType === type" class="active-indicator"></div>
      </div>
    </div>
    
    <!-- 当前餐食列表 -->
    <div class="meals-container">
      <h2 class="section-title">
        今日{{ getMealTypeTitle(selectedMealType) }}
        <span class="calories-badge">
          {{ dietStore[`${selectedMealType}Calories`] }} kcal
        </span>
      </h2>
      
      <div v-if="currentMealList.length === 0" class="empty-state">
        <i class="fas fa-utensils"></i>
        <p>还没有添加{{ getMealTypeTitle(selectedMealType) }}，请从下方推荐餐食中选择或添加自定义餐食</p>
      </div>
      
      <div v-else class="meal-list">
        <div 
          v-for="meal in currentMealList" 
          :key="meal.id"
          class="meal-card"
        >
          <div class="meal-image">
            <img :src="meal.image" :alt="meal.name">
          </div>
          <div class="meal-info">
            <h3>{{ meal.name }}</h3>
            <div class="meal-details">
              <span class="calories">{{ meal.calories }} kcal</span>
              <span class="preparation">{{ meal.preparation }}</span>
            </div>
          </div>
          <button class="remove-btn" @click="removeMeal(meal.id)">
            <i class="fas fa-times"></i>
          </button>
        </div>
      </div>
    </div>
    
    <!-- 推荐餐食 -->
    <div class="recommended-meals">
      <h2 class="section-title">推荐餐食</h2>
      <div class="meal-suggestions">
        <div 
          v-for="meal in dietStore.recommendedMeals" 
          :key="meal.id"
          class="suggestion-card"
          @click="addMeal(meal)"
        >
          <div class="suggestion-image">
            <img :src="meal.image" :alt="meal.name">
          </div>
          <div class="suggestion-info">
            <h3>{{ meal.name }}</h3>
            <p class="description">{{ meal.description }}</p>
            <div class="suggestion-details">
              <span class="calories">{{ meal.calories }} kcal</span>
              <div class="tags">
                <span 
                  v-for="(tag, index) in meal.tags" 
                  :key="index" 
                  class="tag"
                >
                  {{ tag }}
                </span>
              </div>
            </div>
          </div>
          <div class="add-icon">
            <i class="fas fa-plus-circle"></i>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 识别食物 -->
    <div class="scan-fab">
      <i class="fas fa-camera"></i>
    </div>
  </div>
</template>

<style scoped>
.diet-view {
  position: relative;
  padding-bottom: 80px;
}

/* 导航栏 */
.nav-bar {
  padding: 16px;
  background-color: white;
  border-bottom: 1px solid #f0f0f0;
}

.nav-bar h1 {
  margin: 0;
  font-size: 1.25rem;
  color: #333;
  font-weight: 600;
}

/* 卡路里进度 */
.calories-progress {
  background: white;
  border-radius: 12px;
  margin: 16px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.progress-info {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
}

.calories-text {
  font-size: 1.5rem;
  color: #333;
  font-weight: 600;
}

.current {
  color: #16A34A;
}

.separator, .target {
  color: #666;
}

.unit {
  font-size: 0.875rem;
  color: #666;
  margin-left: 4px;
}

.percentage {
  font-size: 1rem;
  color: #16A34A;
  font-weight: 500;
}

.progress-bar {
  height: 8px;
  background-color: #E5E7EB;
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 12px;
}

.progress-filled {
  height: 100%;
  background-color: #16A34A;
  border-radius: 4px;
  transition: width 0.5s ease;
}

.nutrition-info {
  display: flex;
  justify-content: space-between;
}

.nutrition-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.nutrition-item .name {
  font-size: 0.75rem;
  color: #666;
  margin-bottom: 4px;
}

.nutrition-item .value {
  font-size: 0.9375rem;
  color: #333;
  font-weight: 500;
}

/* 餐食选择器 */
.meal-selector {
  display: flex;
  background: white;
  margin: 16px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.meal-type {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px;
  cursor: pointer;
  position: relative;
  color: #666;
}

.meal-type.active {
  color: #16A34A;
  font-weight: 500;
}

.meal-type i {
  font-size: 1.25rem;
  margin-bottom: 4px;
}

.active-indicator {
  position: absolute;
  bottom: 0;
  left: 25%;
  width: 50%;
  height: 3px;
  background-color: #16A34A;
  border-radius: 3px 3px 0 0;
}

/* 当前餐食列表 */
.meals-container {
  margin: 16px;
  background: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.section-title {
  margin: 0 0 16px 0;
  font-size: 1.125rem;
  color: #333;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.calories-badge {
  font-size: 0.875rem;
  background-color: rgba(22, 163, 74, 0.1);
  color: #16A34A;
  padding: 4px 8px;
  border-radius: 16px;
  font-weight: 500;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
  background-color: #f9fafb;
  border-radius: 8px;
  color: #9ca3af;
}

.empty-state i {
  font-size: 2rem;
  margin-bottom: 12px;
}

.empty-state p {
  text-align: center;
  margin: 0;
  font-size: 0.9375rem;
}

.meal-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.meal-card {
  display: flex;
  align-items: center;
  background-color: #f9fafb;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
}

.meal-image {
  width: 80px;
  height: 80px;
  flex-shrink: 0;
}

.meal-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.meal-info {
  flex: 1;
  padding: 12px;
}

.meal-info h3 {
  margin: 0;
  font-size: 1rem;
  color: #333;
  margin-bottom: 4px;
}

.meal-details {
  display: flex;
  justify-content: space-between;
  font-size: 0.875rem;
  color: #666;
}

.remove-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: #9ca3af;
  font-size: 1.125rem;
  cursor: pointer;
  flex-shrink: 0;
  margin-right: 8px;
}

/* 推荐餐食 */
.recommended-meals {
  margin: 16px;
  background: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.meal-suggestions {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.suggestion-card {
  display: flex;
  background-color: #f9fafb;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  cursor: pointer;
}

.suggestion-image {
  width: 120px;
  height: 120px;
  flex-shrink: 0;
}

.suggestion-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.suggestion-info {
  flex: 1;
  padding: 12px;
  position: relative;
}

.suggestion-info h3 {
  margin: 0;
  font-size: 1.125rem;
  color: #333;
  margin-bottom: 4px;
}

.description {
  font-size: 0.875rem;
  color: #666;
  margin: 0 0 8px 0;
  line-height: 1.4;
}

.suggestion-details {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.calories {
  font-size: 0.875rem;
  font-weight: 500;
  color: #16A34A;
}

.tags {
  display: flex;
  gap: 4px;
}

.tag {
  font-size: 0.75rem;
  background-color: rgba(22, 163, 74, 0.1);
  color: #16A34A;
  padding: 2px 6px;
  border-radius: 4px;
}

.add-icon {
  position: absolute;
  bottom: 12px;
  right: 12px;
  color: #16A34A;
  font-size: 1.5rem;
}

/* 扫描按钮 */
.scan-fab {
  position: fixed;
  bottom: 80px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background-color: #16A34A;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  box-shadow: 0 2px 10px rgba(22, 163, 74, 0.4);
  z-index: 10;
  cursor: pointer;
}
</style> 